/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cbir;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

class DCT implements JPEGDecoder.DCTArray {

    //Each entry of this matrix is the DCT coefficient. Recall that the (0,0) of each 8*8 block is the DC coefficient and the rest are AC
    //The corresponding indices are (Y,Cb, Cr), width, height, respectively
    public int[][][] DCTmatrix;
    String file;
    JPEGDecoder j = null;
    // Implementation of DCTArray
    int width, height;

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;

        DCTmatrix = new int[3][height][width];
    }

    public void setPixel(int x, int y, int component, int value) {
        DCTmatrix[component][y][x] = value;
    }

    public static ArrayList<ImageData> colorHistogram(File file, int queryMethod) {



        File folder = new File(file.getParent());
        File[] testImages = folder.listFiles();

        ArrayList<ImageData> imageList = new ArrayList<ImageData>();

        DCT dct1 = new DCT(file.getPath());
        double[] hist1 = Histogram.GetYUVHist(dct1.DCTmatrix, queryMethod);

        DCT[] dct2 = new DCT[100];
        double[] intersection = new double[100];
        int[] sortedOrder = new int[100];
        for (int i = 0; i < 100; i++) {

            dct2[i] = new DCT(testImages[i].getPath());

            double[] hist2 = Histogram.GetYUVHist(dct2[i].DCTmatrix, queryMethod);
            intersection[i] = Histogram.Intersection(hist1, hist2);
           
            imageList.add(new ImageData(testImages[i], intersection[i]));
            boolean lessthan = true;
            for (int j = 0; j < i; j++) {
                if (intersection[i] > intersection[sortedOrder[j]]) {
                    for (int k = i - 1; k >= j; k--) {
                        sortedOrder[k + 1] = sortedOrder[k];
                    }
                    sortedOrder[j] = i;
                    lessthan = false;
                    break;
                }
            }
            if (lessthan == true) {
                sortedOrder[i] = i;
            }
        }
        ArrayList<ImageData> sortedImageList = new ArrayList<ImageData>();

        for (int i = 0; i < 100; i++) {
            sortedImageList.add(imageList.get(sortedOrder[i]));
            System.out.println("" + imageList.get(sortedOrder[i]).intersection + " " + imageList.get(i).intersection);
        }

        return sortedImageList;

    }

    public DCT(String s) {
        file = s;
        j = new JPEGDecoder();




        FileInputStream in = null;

        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            j.decode(in, this);



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

class ImageData {

    File file;
    double intersection;

    public ImageData(File file, double intersection) {
        this.file = file;
        this.intersection = intersection;
    }

    public File getFile() {
        return file;
    }
}
